
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author xingzhiwen
 * @date 2021/9/14
 * @ddescription:
 */
public class SocketTcp01Server {
    public static void main(String[] args) throws IOException {
        //1.在本机的接口9999端口监听。等待连接
        // 细节是9999端口没有被占用
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端在9999端监听，等待连接");
        //当客户端连接9999端口时，程序会 阻塞，等待连接
        //如果客户端连接，则会返回socket对象，程序继续
        Socket socket = serverSocket.accept();
        System.out.println("socket服务端 = " + socket.getClass());
        //通过socket。getinputstream读取客户端写入到数据通道的数据
        InputStream inputStream = socket.getInputStream();
        //io读
        byte[] bytes = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(bytes)) != -1) {
        //根据读取到的长度，显示字符串
            System.out.println(new java.lang.String(bytes, 0, readLen));
        }
        //关闭流
        inputStream.close();
        socket.close();
        serverSocket.close();

    }
}
